package utilidades;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nelson.puerta
 */



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Administrator
 */
public class Conexion {
    private Connection con;
    public Connection getCon() {
        ConexionDB();
        return con;
    }

    public void setCon(Connection con) {
        this.con = con;
    }




    public Conexion(){
    }

    static {
        try{
         //       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();  //conexion para access
                Class.forName("com.mysql.jdbc.Driver").newInstance();

            }catch(ClassNotFoundException e1)
            {
                System.out.println("ClassNotFoundException "+e1.getMessage());
            }
            catch (InstantiationException e2)
            {
               System.out.println("InstantiationException "+e2.getMessage());
            }
            catch (IllegalAccessException e3)
            {
                    System.out.println("IllegalAccessException "+e3.getMessage());
            }catch(Exception e){
                System.out.println("Exception "+e.getMessage());
            }
    }

    public void ConexionDB()
    {
    try{
          //setCon(DriverManager.getConnection("jdbc:odbc:sitioutil","","")); //para access
        setCon(DriverManager.getConnection("jdbc:mysql://localhost:3306/nomina",PropiedadesManager.getInstance().getJdbcProperties("User"),PropiedadesManager.getInstance().getJdbcProperties("Password")));

        }
        catch (SQLException e4){
            System.out.println("SQLException "+e4.getMessage());
        }
        catch (Exception e5){
            System.out.println("otra "+e5.getMessage());
        }
     }

    public static void main(String param[]){
        Conexion conecction = new Conexion();
        try{
            ResultSet r = conecction.getCon().prepareStatement("select * from Empleado").executeQuery();

            if(r.next()){
               System.out.println("id:  "+r.getString(1)+" nombres: "+r.getString(2));
               while(r.next()){
                    System.out.println("codigo:  "+r.getString(1)+" descripcion: "+r.getString(2));

                }

            }else{
                System.out.println("no hay datos");
            }

            //r = conecction.getCon().prepareStatement("select * from PTD.CATegoria").executeQuery();

           // conecction.close();
        }catch(Exception e){
            System.out.println("Excepcion "+e.getMessage());
        }finally{

        }
    }



}